﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI.Design;
using System.Drawing.Design;

[assembly: WebResource("AGuang.Script.AGuang_Client.Controls.InputPrompt.js", "text/javascript")]
[assembly: WebResource("AGuang.Script.AGuang_Client.css.InputPrompt.css", "text/css")]
namespace AGuang.Script {

    /// <summary>
    /// 下拉提示控件
    /// </summary>
    public class InputPrompt:BaseScript {
        private bool autoScript;
        private String forControlID;
        private String inputFocusAjaxUrl;
        private String promptStartAjaxUrl;
        private String onInputFocus;
        private String onPromptStart;
        private int maxListCount=15;
        private String inputFocusAjaxKey = "key";
        private String promptStartAjaxKey = "key";
        private String cssClass;
        private bool bindWidth = true;

        protected override void OnLoad(EventArgs e) {
            ClientScriptManager csm = Page.ClientScript;
            base.OnLoad(e);
            AddStyle(csm.GetWebResourceUrl(this.GetType(),"AGuang.Script.AGuang_Client.css.InputPrompt.css"));
            AddScript(csm.GetWebResourceUrl(this.GetType(), "AGuang.Script.AGuang_Client.Controls.InputPrompt.js"));
            AddScriptContorl(new Action());
            AddScriptContorl(new SAjax());
        }

        protected override void OnPreRender(EventArgs e) {
            base.OnPreRender(e);
            if (!AutoScript) return;
            if (String.IsNullOrEmpty(ForControlID)) throw new Exception("请指定“服务器控件编号”或“客户端控件编号”");
            Control c = this.Parent.FindControl(ForControlID);
            if (c != null) ForControlID = c.ClientID;
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("var {0}=new Page.InputPrompt('{1}',{2});\n", JsVar, ForControlID, MaxListCount);
            if (!String.IsNullOrEmpty(OnInputFocus)) {
                sb.AppendFormat("{0}.Add_InputFocus({1});\n", JsVar, OnInputFocus);
            }
            if (!String.IsNullOrEmpty(OnPromptStart)) {
                sb.AppendFormat("{0}.Add_StartFun({1});\n", JsVar, OnPromptStart);
            }
            if (!String.IsNullOrEmpty(PromptStartAjaxUrl)) {
                String PromptStrartFunction = JsVar + "__PromptStartAjaxUrl__Fun";
                String PromptStrartAjaxVar=JsVar + "__PromptStartAjaxUrl__Ajax";
                String PromptStrartAjaxGetData=JsVar + "__PromptStartAjaxUrl__AjaxGetData";
                String PromptStrartUrl=Page.ResolveUrl(PromptStartAjaxUrl);
                sb.AppendFormat("var {0}=new Super.Ajax('{1}',{2});", PromptStrartAjaxVar, PromptStrartUrl, PromptStrartAjaxGetData);
                sb.AppendFormat("{0}.Add_StartFun({1});", JsVar, PromptStrartFunction);
                sb.AppendFormat("function {0}(sender,e){{", PromptStrartFunction);
                sb.AppendFormat("{0}.Send({{{1}:e}});", PromptStrartAjaxVar, PromptStartAjaxKey);
                sb.Append("}");
                sb.AppendFormat("function {0}(sender,e){{", PromptStrartAjaxGetData);
                sb.Append("try{");
                sb.Append("var obj=e.responseJSON();");
                sb.AppendFormat("{0}.Fill(obj);", JsVar);
                sb.Append("}catch(e){}");
                sb.Append("}");
            }
            if (!String.IsNullOrEmpty(InputFocusAjaxUrl)) {
                String inputFocusFunction = JsVar + "__InputFocusAjaxUrl__Fun";
                String inputFocusAjaxVar = JsVar + "__InputFocusAjaxUrl__Ajax";
                String inputFocusAjaxGetData = JsVar + "__InputFocusAjaxUrl__AjaxGetData";
                String inputFocusUrl = Page.ResolveUrl(PromptStartAjaxUrl);
                sb.AppendFormat("var {0}=new Super.Ajax('{1}',{2});", inputFocusAjaxVar, inputFocusUrl, inputFocusAjaxGetData);
                sb.AppendFormat("{0}.Add_InputFocus({1});", JsVar, inputFocusFunction);
                sb.AppendFormat("function {0}(sender,e){{", inputFocusFunction);
                sb.AppendFormat("{0}.Send({{{1}:e}});", inputFocusAjaxVar, PromptStartAjaxKey);
                sb.Append("}");
                sb.AppendFormat("function {0}(sender,e){{", inputFocusAjaxGetData);
                sb.Append("try{");
                sb.Append("var obj=e.responseJSON();");
                sb.AppendFormat("{0}.Fill(obj);", JsVar);
                sb.Append("}catch(e){}");
                sb.Append("}");
            }
            if (!String.IsNullOrEmpty(CssClass)) {
                sb.AppendFormat("{0}.SetCssClass('{1}');", JsVar, CssClass);
            }
            if (BindWidth) {
                sb.AppendFormat("{0}.BindWidth();", JsVar);
            }
            AddEffect(sb.ToString());
        }

        /// <summary>
        /// 提示的最多显示数量
        /// </summary>
        [Description("提示的最多显示数量")]
        [Category("初始化")]
        [DefaultValue(15)]
        public int MaxListCount {
            get { return maxListCount; }
            set { maxListCount = value; }
        }

        /// <summary>
        /// 文本框被激活时取数据的路径,实现比OnInputFocus更深一层的处理
        /// </summary>
        [Description("文本框被激活时取数据的路径,实现比OnInputFocus更深一层的处理")]
        [Category("获取数据")]
        [Editor(typeof(UrlEditor),typeof(UITypeEditor))]
        public String InputFocusAjaxUrl {
            get { return inputFocusAjaxUrl; }
            set { inputFocusAjaxUrl = value; }
        }

        /// <summary>
        /// ajax传值用的参数名称
        /// </summary>
        [Description("ajax传值用的参数名称")]
        [Category("获取数据")]
        public String PromptStartAjaxKey {
            get { if (String.IsNullOrEmpty(PromptStartAjaxUrl)) return String.Empty; return promptStartAjaxKey; }
            set { promptStartAjaxKey = value; }
        }

        /// <summary>
        /// 启动提示时取数据的路径，实现比OnPromptStart更深一层的处理
        /// </summary>
        [Description("启动提示时取数据的路径，实现比OnPromptStart更深一层的处理")]
        [Category("获取数据")]
        [Editor(typeof(UrlEditor), typeof(UITypeEditor))]
        public String PromptStartAjaxUrl {
            get { return promptStartAjaxUrl; }
            set { promptStartAjaxUrl = value; }
        }

        /// <summary>
        /// ajax传值用的参数名称
        /// </summary>
        [Description("ajax传值用的参数名称")]
        [Category("获取数据")]
        public String InputFocusAjaxKey {
            get { if (String.IsNullOrEmpty(InputFocusAjaxUrl)) return String.Empty; return inputFocusAjaxKey; }
            set { inputFocusAjaxKey = value; }
        }


        /// <summary>
        /// 文本框被激活时产生的事件
        /// </summary>
        [Description("文本框被激活时产生的事件")]
        [Category("客户端事件")]
        public String OnInputFocus {
            get { return onInputFocus; }
            set { onInputFocus = value; }
        }

        /// <summary>
        /// 启动提示时产生的事件
        /// </summary>
        [Description("启动提示时产生的事件")]
        [Category("客户端事件")]
        public String OnPromptStart {
            get { return onPromptStart; }
            set { onPromptStart = value; }
        }

        /// <summary>
        /// 文本框的id
        /// </summary>
        [Description("文本框的id")]
        [Category("初始化")]
        public String ForControlID {
            get { return forControlID; }
            set { forControlID = value; }
        }

        /// <summary>
        /// 启用自动生成脚本
        /// </summary>
        [Description("启用自动生成脚本")]
        [Category("脚本控制")]
        public bool AutoScript {
            get { return autoScript; }
            set { autoScript = value; }
        }

        /// <summary>
        /// 脚本实例名称
        /// </summary>
        [Description("脚本实例名称")]
        [Category("脚本控制")]
        public String JsVar {
            get { return "s" + this.ClientID; }
        }

        /// <summary>
        /// 设置样式
        /// </summary>
        [Description("设置样式")]
        [Category("外观")]
        public String CssClass {
            get { return cssClass; }
            set { cssClass = value; }
        }

        /// <summary>
        /// 将提示框设为和文本框的宽度一样
        /// </summary>
        [Description("将提示框设为和文本框的宽度一样")]
        [Category("外观")]
        public bool BindWidth {
            get { return bindWidth; }
            set { bindWidth = value; }
        }

    }
}
